package com.rj.bd.kj.fdc.dsa;


import com.rj.bd.kj.fdc.MyAbstractClassFDC;
import com.rj.bd.kj.utils.MyExceptionUtils;

import java.security.*;

public class DSASignatureImpl extends MyAbstractClassFDC {
    private PrivateKey privateKey;
    private PublicKey publicKey;
    private Signature signature;

    public DSASignatureImpl(KeyPair keyPair)  {

        try {
            this.privateKey = keyPair.getPrivate();
            this.publicKey = keyPair.getPublic();
            this.signature = Signature.getInstance("SHA256withDSA");
        } catch (Exception e) {
            MyExceptionUtils.handlerExceptions(e);
        }
    }


    public byte[] encrypt(byte[] data)  {
        try {
            signature.initSign(privateKey);
            signature.update(data);
            return signature.sign();
        }catch (Exception e){
            MyExceptionUtils.handlerExceptions(e);
        }
        return null;
    }



    // 辅助方法：将字节数组转换为十六进制字符串
    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }


    @Override
    public KeyPair generateKeyPair(int keySize, String keyFormat)  {
        return null;
    }

    @Override
    public byte[] encrypt(byte[] data, String publicKeyStr, String charset) throws Exception {
        return new byte[0];
    }

    @Override
    public byte[] decrypt(byte[] encryptedData, String privateKeyStr, String charset) throws Exception {
        return new byte[0];
    }

    @Override
    public byte[] encrypt(byte[] originalBytes, PublicKey publicKey, String s)  {
        return new byte[0];
    }

    @Override
    public byte[] decrypt(byte[] eData, PrivateKey privateKey, String s)  {
        return new byte[0];
    }
}